package com.neusoft.elmboot.controller;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;

import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import com.neusoft.elmboot.po.Food;
import com.neusoft.elmboot.service.FoodService;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/FoodController")
public class FoodController {
	
	@Autowired
	private FoodService foodService;

	@RequestMapping("/listFoodByBusinessId")
	public List<Food> listFoodByBusinessId(Food food) throws Exception{
		return foodService.listFoodByBusinessId(food.getBusinessId());
	}

	@RequestMapping("/removeFood")
	public int removeFood(Food food, HttpServletRequest request, String userId) throws Exception {
		String token = null;
		if (request.getCookies() != null) {
			for (Cookie cookie : request.getCookies()) {
				if (cookie.getName().equals("token")) {
					token = cookie.getValue();
					break;
				}
			}
		}

		// 验证 token 和 userId 一致性
		if (token != null && token.equals(userId)) {
			return foodService.removeFood(food.getFoodId());
		} else if (token == null) {
			return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("缺少验证信息").getStatusCodeValue();
		}else{
			return ResponseEntity.status(HttpStatus.FORBIDDEN).body("权限不足").getStatusCodeValue();
		}
	}

	@RequestMapping("/addFood")
	public ResponseEntity<String> addFood(@RequestParam("foodName") String foodName,
										  @RequestParam("foodExplain") String foodExplain,
										  @RequestParam(value = "foodImg", required = false) MultipartFile foodImg,
										  @RequestParam("foodPrice") Double foodPrice,
										  @RequestParam("businessId") Integer businessId) {
		try {
			Food food = new Food();
			food.setFoodName(foodName);
			food.setFoodExplain(foodExplain);
			food.setFoodPrice(foodPrice);
			food.setBusinessId(businessId);

			if (foodImg != null && !foodImg.isEmpty()) {
				// 处理图片保存逻辑
				String imgPath = "/path/to/save/image";  // 假设保存路径
				food.setFoodImg(imgPath);
			} else {
				food.setFoodImg(null);  // 图片为空时设为 null
			}

			int result = foodService.addFood(food);

			if (result > 0) {
				return ResponseEntity.ok("新增食品成功！");
			} else {
				return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("新增食品失败！");
			}
		} catch (Exception e) {
			return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("服务器内部错误：" + e.getMessage());
		}
	}

	@PostMapping("/updateFoodPrice")
	public ResponseEntity<String> updateFoodPrice(@RequestBody Food food) {
		try {
			int result = foodService.updateFoodPrice(food);
			if (result > 0) {
				return ResponseEntity.ok("商品价格更新成功");
			} else {
				return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("更新失败，找不到对应的商品");
			}
		} catch (Exception e) {
			e.printStackTrace();
			return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("服务器内部错误：" + e.getMessage());
		}
	}

}
